package com.app.newiptv.adaper;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.app.newiptv.R;
import com.app.newiptv.activity.Contast;
import com.app.newiptv.activity.SitcomActivity;
import com.app.newiptv.base.MyApplication;
import com.app.newiptv.presenter.SitcomOrderPresenter;

import java.util.Arrays;

/**
 * Created by Shinelon on 2017/9/15.
 */

public class SitcomAdapter extends RecyclerView.Adapter<SitcomAdapter.MainRecycleViewHolder> {
    private SitcomOrderPresenter presenter;
    private int count;
    private String[] sitcomIndex;
    private String sitcomName;
    private SQLiteDatabase wirteDatabase;
    private Context context;
    public SitcomAdapter(SitcomOrderPresenter presenter, int count, Context context) {
        this.presenter = presenter;
        this.count = count;
        this.context = context;
    }

    public void setSitcomIndex(String[] sitcoms) {
        sitcomIndex = sitcoms;
    }

    public void setSitcomName(String name) {
        sitcomName = name;
    }

    public void setWriteDatabase(SQLiteDatabase database) {
        wirteDatabase = database;
    }

    @Override
    public MainRecycleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_sitcom_view, parent, false);
        MainRecycleViewHolder mainRecycleViewHolder = new MainRecycleViewHolder(view);
        return mainRecycleViewHolder;
    }

    @Override
    public void onBindViewHolder(MainRecycleViewHolder holder, int position) {
        presenter.onBindSitcomChanelView(holder, position);
        holder.setPosition(position);
    }

    public void refresh()
    {

    }

    @Override
    public int getItemCount() {
        return count;
    }

    public class MainRecycleViewHolder extends RecyclerView.ViewHolder implements SitcomOrderPresenter.MainViewSitcomHolder {

        private TextView mBtn_xj;
        private int position;
        private String[] sitcomArr;
        private String index;

        public MainRecycleViewHolder(View itemView) {
            super(itemView);
            mBtn_xj = (TextView) itemView.findViewById(R.id.btn_xj);
            mBtn_xj.setTypeface(MyApplication.face);
            itemView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view, boolean b) {
                    if (b) {
                        mBtn_xj.setBackgroundResource(R.mipmap.se_item_bg_select);
                    } else {
                        boolean isExis = false;
                        if (sitcomArr != null && sitcomArr.length > 0) {
                            for (int i = 0; i < sitcomArr.length; i++) {
                                if (index.equals(sitcomArr[i])) {
                                    isExis = true;
                                }
                            }
                        }
                        setExistCorlor(isExis);

                    }
                }
            });
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (Contast.USE_GROUP.equals("3")) {
                        String indexText = mBtn_xj.getText().toString().trim();
                        if (sitcomIndex== null) {
                            String sitcom_index = indexText + ",";
                            ContentValues values = new ContentValues();
                            values.put("name", sitcomName);
                            values.put("sitcom_index", sitcom_index);
                            Log.i("legendki", "onClick: " + values);
                            wirteDatabase.insert("SitcomIndex",null,values);
                        }
                        else if (sitcomIndex!= null && !Arrays.asList(sitcomIndex).contains(indexText)) {
                            String sitcom_index = "";
                            for (int i = 0; i < sitcomIndex.length; i++) {
                                sitcom_index = sitcom_index + sitcomIndex[i] + ",";
                            }
                            sitcom_index = sitcom_index + indexText + ",";
                            ContentValues values = new ContentValues();
                            values.put("name", sitcomName);
                            values.put("sitcom_index", sitcom_index);
                            Log.i("legendki", "onClick: " + values);
                            wirteDatabase.update("SitcomIndex", values, "name=" + "'" + sitcomName + "'", null);
                        }
                        presenter.toPlay(position);
                    } else {
                        Toast.makeText(context, "您不是VIP", Toast.LENGTH_SHORT).show();
                    }

                }
            });
        }

        private void setExistCorlor(boolean isExis)
        {
            if (isExis) {
                mBtn_xj.setTextColor(mBtn_xj.getContext().getResources().getColor(R.color.played));
                mBtn_xj.setBackgroundResource(R.mipmap.se_item_bg_focused);
            } else {
                mBtn_xj.setTextColor(mBtn_xj.getContext().getResources().getColor(R.color.white));
                if (sitcomIndex!= null && Arrays.asList(sitcomIndex).contains(mBtn_xj.getText().toString().trim())) {
                    mBtn_xj.setBackgroundResource(R.drawable.button_border);
                } else {
                    mBtn_xj.setBackgroundResource(R.mipmap.se_item_bg_normal);
                }
            }

        }

        @Override
        public void setNumber(String index, boolean isExist ) {
            Log.i("sitcomIndex", "setNumber: " + index);
            if (index != null) {
                this.index = index;
                setExistCorlor(isExist);
                int textLength = index.length();
                if (textLength < 3) {
                    mBtn_xj.setTextSize(16);
                } else if (textLength >= 3 && textLength <= 8) {
                    mBtn_xj.setTextSize(14);
                } else {
                    mBtn_xj.setTextSize(12);
                }
                mBtn_xj.setText(index);
                if (sitcomIndex!= null && Arrays.asList(sitcomIndex).contains(index)) {
                    mBtn_xj.setBackgroundResource(R.drawable.button_border);
                } else {
                    mBtn_xj.setBackgroundResource(R.mipmap.se_item_bg_normal);
                }
            }
        }

        public void setPosition(int position) {
            this.position = position;
        }
    }
}
